package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.Board;
import com.example.mapper.BoardMapper;
import com.example.utils.ResultData;
import com.example.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * The type Board service.
 */
@Service
public class BoardService {
    @Autowired
    private BoardMapper mapper;

    /**
     * Gets board.
     *
     * @return the board
     */
    public ResultData getBoard() {
        QueryWrapper<Board> queryWrapper = new QueryWrapper<Board>();
        queryWrapper.select().orderByDesc("id");
        List<Board> res = mapper.selectList(queryWrapper);
        return ResultUtils.sucuss("查询成功", res);
    }

    /**
     * Add board result data.
     *
     * @param board the board
     * @return the result data
     */
    public ResultData addBoard(Board board) {
        String username = board.getUserId();
        if (username.isEmpty()) {
            board.setUserId("游客");
        }
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String date1 = sdf.format(date);
        board.setReleaseTime(date1);
        int res = mapper.insert(board);
        if (res > 0) {
            return ResultUtils.sucuss("发表成功", "影响行数为" + res);
        } else {
            return ResultUtils.error("发表失败，请重试！", "影响行数为" + res);
        }
    }


}
